﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using Comm;
using BusinessFacade;
using Entities;
using System.Collections;

namespace MedicareNoteManagerWeb.Web
{
    public partial class Persongroupmanage : BasePage
    {
      
        med_persongroupmanage_Model model1 = new med_persongroupmanage_Model();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                model1 = (med_persongroupmanage_Model)Session["PersonGroupManageModel"];
                BindDrop();
                LoadList();
                lbl_Message.Text = "";
            }
        }
        public void BindDrop()
        {
            FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", this.DropArea);
        }

        public void LoadList()
        {
            Busi_med_persongroupmanage users = new Busi_med_persongroupmanage();
            DataTable dt = users.GetList(txt_PersonState.Text.Trim(), int.Parse(DropArea.SelectedValue));
            Grd_User.DataSource = dt;
            Grd_User.DataBind();
        }

        protected void Grd_User_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Grd_User.EditIndex = e.NewEditIndex;
            LoadList();
        }
        protected void Grd_User_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Grd_User.EditIndex = -1;
            LoadList();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadList();
        }



        protected void Grd_User_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //重新绑定
            LoadList();
        }


        protected void Grd_User_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
            try
            {
                Busi_med_persongroupmanage.Delete(ID);
                MessageBox.ShowDialog(Page, "删除成功！", "success");
                LoadList();
            }
            catch (Exception ex)
            {
                MessageBox.ShowDialog(Page, "删除失败！" + ex.Message, "error");
                LoadList();
            }
        }


        protected void Grd_User_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DropDownList ddlEarth = new DropDownList();
            ddlEarth = (DropDownList)e.Row.FindControl("ddlEarth");
            if (ddlEarth != null)
            {
                FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", ddlEarth);
                int Id = Convert.ToInt32(Grd_User.DataKeys[e.Row.RowIndex].Values[0].ToString());
                Busi_med_persongroupmanage u = new Busi_med_persongroupmanage();
                med_persongroupmanage_Model model = u.GetListModel(Id);
                ddlEarth.SelectedValue = model.Area.ToString();
            }

            DropDownList cbosubordinateunits = new DropDownList();
            cbosubordinateunits = (DropDownList)e.Row.FindControl("cbosubordinateunits");
            if (ddlEarth != null)
            {
                cbosubordinateunits.Items.Add("请选择企业单位");
                cbosubordinateunits.Items.Add("国营");
                cbosubordinateunits.Items.Add("私营");
                int Id = Convert.ToInt32(Grd_User.DataKeys[e.Row.RowIndex].Values[0].ToString());
                Busi_med_persongroupmanage u = new Busi_med_persongroupmanage();
                med_persongroupmanage_Model model = u.GetListModel(Id);
                cbosubordinateunits.SelectedValue = model.SubordinateUnits.ToString();
            }

            DropDownList cbopersonsex = new DropDownList();
            cbopersonsex = (DropDownList)e.Row.FindControl("cbopersonsex");
            if (ddlEarth != null)
            {
                cbopersonsex.Items.Add("请选择性别");
                cbopersonsex.Items.Add("男");
                cbopersonsex.Items.Add("女");
                int Id = Convert.ToInt32(Grd_User.DataKeys[e.Row.RowIndex].Values[0].ToString());
                Busi_med_persongroupmanage u = new Busi_med_persongroupmanage();
                med_persongroupmanage_Model model = u.GetListModel(Id);
                cbopersonsex.SelectedValue = model.PersonSex.ToString();
            }

            DropDownList cbopersonstate = new DropDownList();
            cbopersonstate = (DropDownList)e.Row.FindControl("cbopersonstate");
            if (ddlEarth != null)
            {
                cbopersonstate.Items.Add("请选择用户状态");
                cbopersonstate.Items.Add("欠费");
                cbopersonstate.Items.Add("缴费");
                int Id = Convert.ToInt32(Grd_User.DataKeys[e.Row.RowIndex].Values[0].ToString());
                Busi_med_persongroupmanage u = new Busi_med_persongroupmanage();
                med_persongroupmanage_Model model = u.GetListModel(Id);
                cbopersonstate.SelectedValue = model.PersonState.ToString();
                ArrayList list = new ArrayList();
                DbHelperMySql.ExecuteSqlTran(list);
            }
        }

        protected void Grd_User_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                med_persongroupmanage_Model model = new med_persongroupmanage_Model();
                model.GroupId = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
                model.PaymentBase = Convert.ToDecimal(((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtpaymentbase")).Text);
                model.PaymentTerm = Convert.ToInt32(((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtpaymentterm")).Text);
                model.PersonAge = Convert.ToInt32(((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtpersonage")).Text);
                model.PersonSex = (((DropDownList)Grd_User.Rows[e.RowIndex].FindControl("cbopersonsex")).SelectedValue);
                model.PersonState = (((DropDownList)Grd_User.Rows[e.RowIndex].FindControl("cbopersonstate")).SelectedValue);
                model.SubordinateUnits = (((DropDownList)Grd_User.Rows[e.RowIndex].FindControl("cbosubordinateunits")).SelectedValue);
                model.Area = Convert.ToInt32(((DropDownList)Grd_User.Rows[e.RowIndex].FindControl("ddlEarth")).SelectedValue);
                model.ArrearageTerm = Convert.ToInt32(((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtarrearageterm")).Text);
                if (model.Area == 0)
                {
                    MessageBox.ShowDialog(Page, "请选择区域！", "error");
                    return;
                }
                if (model.PersonAge==0)
                {
                    MessageBox.ShowDialog(Page, "请填填写年龄！", "error");
                    return;
                }
                //if (model.PlanExplain == "")
                //{
                //    MessageBox.ShowDialog(Page, "请填填写计划发送说明！", "error");
                //    return;
                //}
                //if (model.NoteContent == "")
                //{
                //    MessageBox.ShowDialog(Page, "请填填写计划发送内容！", "error");
                //    return;
                //}
                //if (model.SendTime == null)
                //{
                //    MessageBox.ShowDialog(Page, "请填填写计划发送时间！", "error");
                //    return;
                //}
                else
                {
                    new Busi_med_persongroupmanage().Update(model);
                    MessageBox.ShowDialog(Page, "修改成功！", "success");
                }
                Grd_User.EditIndex = -1;
                LoadList();
            }
            catch (Exception ex)
            {
                lbl_Message.Text = ex.Message;
                MessageBox.ShowDialog(Page, "修改失败！" + ex.Message, "error");
            }
        }
    }
}
